package net.alteiar.images.filter;

import java.awt.Color;

import net.alteiar.images.metadata.IImageInfo;
import net.alteiar.transformation.TransformationFunction;

public class ClusterFilter extends BasicFilter {

	private final ColorTransformation fct;

	private class ColorTransformation implements TransformationFunction {
		private final int[] color;

		public ColorTransformation(Color[] init) {
			color = new int[init.length];
			for (int i = 0; i < init.length; i++) {
				color[i] = init[i].getRGB();
			}
		}

		public void setColor(int i, Color c) {
			color[i] = c.getRGB();
		}

		public Color getColor(int i) {
			return new Color(color[i]);
		}

		public int getColorCount() {
			return color.length;
		}

		@Override
		public int transform(int value) {
			return color[value];
		}

	}

	public ClusterFilter(IImageInfo inf, Color[] colors) {
		super(inf);
		fct = new ColorTransformation(colors);
		updateFilter();
	}

	public void setColorAt(int i, Color c) {
		fct.setColor(i, c);
		updateFilter();
		this.setChanged();
		this.notifyObservers();
	}

	public Color getColor(int i) {
		return fct.getColor(i);
	}

	public int getClusterCount() {
		return fct.getColorCount();
	}

	@Override
	public void updateFilter() {
		int[] intImage = layer.getImagePixels();
		info.getValues(intImage, fct);
	}
}
